{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 4, 4.125)\n",
      "<class 'tuple'>\n",
      "I am apples\n",
      "1 4 4.125\n",
      "1\n",
      "(1, 4)\n",
      " \n"
     ]
    }
   ],
   "source": [
    "\n",
    "#创建元祖\n",
    "example_turple = (1,4,4.125)\n",
    "\n",
    "example_turples = ()\n",
    "\n",
    "print(example_turple)\n",
    "print(type(example_turple))\n",
    "##  列表，字符串 ,\n",
    "apple =\"I am apple \"\n",
    "apple =\"I am apples\"\n",
    "print(apple)\n",
    "\n",
    "# 讲元祖的值进行解构分别赋予给三个变量\n",
    "first,two,third = example_turple\n",
    "print(first,two,third)\n",
    "\n",
    "print(example_turple[0])\n",
    "print(example_turple[0:2])\n",
    "\n",
    "example_turple = (1,4,4.125)\n",
    "\n",
    "# 元祖不能修改里面元素的值,强制修改会报错\n",
    "\n",
    "#example_turple[1] = 5\n",
    "\n",
    "print(apple[1])\n",
    "\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'月球', '水星', '地球', '金星'} {'水星', '海王星', '冥王星', '天王星', '月球', '地球'} <class 'set'> <class 'set'>\n",
      "这里是并集的操作演示\n",
      "{'水星', '海王星', '冥王星', '天王星', '金星', '月球', '地球'}\n",
      "这是交集的操作\n",
      "这是交集 {'月球', '水星', '地球'}\n",
      "这是差集的操作\n",
      "这是存在Names2不存在names的元素 {'天王星', '海王星', '冥王星'}\n",
      "{'金星'}\n",
      "{'海王星', '天王星', '冥王星', '金星'}\n"
     ]
    }
   ],
   "source": [
    "# 集合的创建  set  \n",
    "\n",
    "# list()  set() dict()  str()\n",
    "# []  {} {}  \"\" \n",
    "\n",
    "names  = {\"地球\",\"月球\",\"水星\",\"月球\",\"金星\"}\n",
    "\n",
    "names2 = set([\"地球\",\"月球\",\"水星\",\"月球\",\"天王星\",\"海王星\",\"冥王星\"])\n",
    "\n",
    "print(names,names2,type(names),type(names))\n",
    "\n",
    "# | &  - ^  \n",
    "print(\"这里是并集的操作演示\")\n",
    "namesa = names | names2   #  集合name 和集合names2 大家都有的元素之外的元素既然包含names,又包含names2\n",
    "print(namesa)\n",
    "print(\"这是交集的操作\")\n",
    "namesb = names & names2 \n",
    "print(\"这是交集\",namesb)\n",
    "\n",
    "print(\"这是差集的操作\")\n",
    "#  vlook up \n",
    "\n",
    "namesc  = names2 - names  # 在names2里面存在但是在names里面不存在的元素\n",
    "print(\"这是存在Names2不存在names的元素\",namesc)\n",
    "\n",
    "namesd = names-names2  # excle vlookup  我选取A 列,我又选取了B列,\n",
    "print(namesd)\n",
    "\n",
    "namesz = names ^ names2  # 仅仅存在names的元素和存在names2的元素 \n",
    "print(namesz)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "添加了水果元素 {'水星', '海王星', '冥王星', '天王星', '金星', '月球', '地球', '水果'}\n",
      "批量添加元素update {'水星', '海王星', '冥王星', '天王星', '金星', '月球', '地球', '水果'}\n"
     ]
    }
   ],
   "source": [
    "## 集合元素添加  add update  一个添加单个元素,一个添加批量元素\n",
    "\n",
    "names.add(\"水果\") #set \n",
    "print(\"添加了水果元素\",names)\n",
    "names.update(names2)\n",
    "final_names = names\n",
    "print(\"批量添加元素update\",final_names)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 集合元素的移除  remove discard "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'水星', '海王星', '冥王星', '天王星', '月球', '地球', '水果'}\n"
     ]
    }
   ],
   "source": [
    "#names.remove(\"谷神星\")#KeyError: '谷神星'\n",
    "names.discard('谷神星')\n",
    "\n",
    "names.discard('金星')\n",
    "print(names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'7125': '苹果', '7126': '香蕉', '7127': '橘子'}\n",
      "苹果\n"
     ]
    }
   ],
   "source": [
    "##  字典的创建\n",
    "#{} dict()\n",
    "\n",
    "# {'key':\"value\"}\n",
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\"}\n",
    "print(trans_code)\n",
    "\n",
    "first_value  = trans_code['7125']\n",
    "print(trans_code['7125'])\n",
    "\n",
    "value = input(\"请输入你需要查询的code: \")\n",
    "\n",
    "print(trans_code[value])\n",
    "\n",
    "# 真\n",
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\"}\n",
    "# try:\n",
    "#     if value in trans_code:\n",
    "#         print(trans_code[value])\n",
    "    \n",
    "#     else:\n",
    "#         print(\"您要查询的值存在,请联系管理员进行添加\")\n",
    "#     # 假\n",
    "# Exception as e：\n",
    "#     print(e)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "7125",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[3], line 3\u001b[0m\n\u001b[0;32m      1\u001b[0m trans_code  \u001b[38;5;241m=\u001b[39m{\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m7125\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m苹果\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m7126\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m香蕉\u001b[39m\u001b[38;5;124m\"\u001b[39m,\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m7127\u001b[39m\u001b[38;5;124m\"\u001b[39m:\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m橘子\u001b[39m\u001b[38;5;124m\"\u001b[39m}\n\u001b[1;32m----> 3\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[43mtrans_code\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m7125\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[0;32m      4\u001b[0m \u001b[38;5;28mprint\u001b[39m(trans_code)\n",
      "\u001b[1;31mKeyError\u001b[0m: 7125"
     ]
    }
   ],
   "source": [
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\"}\n",
    "\n",
    "del trans_code['7125']\n",
    "print(trans_code)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_values(['苹果', '香蕉', '橘子'])\n",
      "dict_keys(['7125', '7126', '7127'])\n"
     ]
    }
   ],
   "source": [
    "#获取字典里面所有的键和所有的值\n",
    "\n",
    "total_value = trans_code.values()\n",
    "print(total_value)\n",
    "\n",
    "total_key = trans_code.keys()\n",
    "print(total_key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 集合元祖字典 \n",
    "## 集合的使用 ,元祖的使用，字典的使用 \n",
    "##  - - \n",
    "## 元祖的创建与访问 元素的赋值结构表达式\n",
    "\n",
    "#  {} 字典的创建，字典的访问 dict.value , dict.keys() \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'7125': '苹果', '7126': '香蕉', '7127': '橘子', '220': '不存在'}\n"
     ]
    }
   ],
   "source": [
    "trans_code  ={\"7125\":\"苹果\",\"7126\":\"香蕉\",\"7127\":\"橘子\"}\n",
    "value = input(\"请输入你需要查询的code: \")\n",
    "\n",
    "if value in trans_code:\n",
    "    print(trans_code[value])\n",
    "    \n",
    "else:  \n",
    "    trans_code[value]='不存在'\n",
    "    print(trans_code[value])\n",
    "    #print(\"您要查询的值存在,请联系管理员进行添加\")\n",
    "\n",
    "##  for 循环\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
